package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.enums.UserRelationType;
import com.yuantu.judiciary.model.form.*;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.ICheckEnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 企业尽调模块
 *
 * @author syw
 * @since 2023-07-13
 **/
@RestController
@RequestMapping(value = "/check-enterprise")
@Api(tags = "企业尽调模块")
@RequiredArgsConstructor
public class CheckEnterpriseController extends BaseController {

    private final ICheckEnterpriseService enterpriseService;

    @ApiOperation(value = "获取模糊搜索企业名称列表")
    @PostMapping("/get/enterprise-list")
    public Response<PageVO<EntListVO>> getEnterpriseList(@PageableDefault Pageable pageable,
                                                         @NotBlank(message = "企业名称不能为空") @RequestParam String enterpriseName) {
        return ResponseFactory.okResponse(enterpriseService.getEnterpriseList(enterpriseName, pageable));
    }

    @ApiOperation(value = "上传授权文件")
    @PostMapping(value = "/upload/authorized-file")
   // @ApiImplicitParam(name = "file", value = "上传的文件",  dataTypeClass = MultipartFile.class, required = true)
    public Response<String> uploadAuthorizedFile(@RequestPart(value = "file") MultipartFile file, @RequestPart(value = "name") String name) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.uploadAuthorizedFile(userId, file, name));
    }


    @ApiOperation(value = "添加授权记录")
    @PostMapping("/add/authorized-record")
    public Response<Long> addAuthorizedRecord(@RequestBody @Validated AddEntAuthRecordForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.addAuthorizedRecord(userId, form));
    }


    @ApiOperation(value = "微信小程序直接生成查询企业授权记录")
    @PostMapping(value = "/ent/auth")
    public Response<Long> entAuthQueryRecord(@RequestParam (value = "name") String name, @RequestParam(required = false) String codeInfo) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.entAuthQueryRecord(userId, name, codeInfo));
    }


    @ApiOperation(value = "默认授权")
    @PostMapping(value = "/upload/authorized-file/default")
    public Response<String> uploadAuthorizedFile(@RequestParam(value = "name") String name) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.uploadAuthorizedFile(userId, null, name));
    }

    @ApiOperation(value = "上传授权文件-阿里小程序")
    @PostMapping(value = "/upload/authorized-file/ali")
    //@ApiImplicitParam(name = "file", value = "上传的文件", dataType = "_File", dataTypeClass = MultipartFile.class, required = true)
    public Response<Long> uploadAuthorizedFileByAli(@RequestPart(value = "file") MultipartFile file, @RequestPart(value = "name") String name) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.uploadAuthorizedFileByAli(userId, file, name));
    }


    @ApiOperation(value = "获取摘要")
    @PostMapping("/get/brief")
    public Response<CheckEnterpriseBriefVO> getBrief(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getBrief(userId, form));
    }

    @ApiOperation(value = "获取概览")
    @PostMapping("/get/overview")
    public Response<CheckEnterpriseOverviewVO> getOverview(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getOverview(userId, form));
    }

    @ApiOperation(value = "获取详情-司法信息")
    @PostMapping("/get/detail/judicial-info")
    public Response<CheckEnterpriseJudicialVO> getDetailByJudicialInfo(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getDetailByJudicialInfo(userId, form));
    }

    @ApiOperation(value = "获取详情-司法信息案件数量")
    @PostMapping("/get/detail/judicial-info/num")
    public Response<CheckEnterpriseJudicialVO> getDetailByJudicialInfoNum(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getDetailByJudicialInfoNum(userId, form));
    }

    @ApiOperation(value = "获取详情-司法信息（分页）")
    @PostMapping("/get/detail/judicial-info/page")
    public Response<PageVO<Object>> getDetailByJudicialInfoPage(@PageableDefault Pageable pageable,
                                                                @RequestBody @Validated QueryCasePageForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getDetailByJudicialInfoPage(userId, form, pageable));
    }

    @ApiOperation(value = "获取详情-经营风险")
    @PostMapping("/get/detail/business-risk")
    public Response<CheckEnterpriseBusinessRiskVO> getDetailByBusinessRisk(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getDetailByBusinessRisk(userId, form));
    }

    @ApiOperation(value = "获取详情-工商信息")
    @PostMapping("/get/detail/business-info")
    public Response<CheckEnterpriseBusinessInfoVO> getDetailByBusinessInfo(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getDetailByBusinessInfo(userId, form));
    }

    @ApiOperation(value = "获取详情-年报")
    @PostMapping("/get/detail/report")
    public Response<CheckEnterpriseYearReportVO> getDetailByReport(@RequestBody @Validated GetReportInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getDetailByReport(userId, form));
    }

    @ApiOperation(value = "图谱-客户")
    @GetMapping("/get/atlas/customer")
    public Response<List<UserRelationVO>> getCustomerAtlas() {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getRelationAtlas(userId, UserRelationType.CUSTOMER));
    }

    @ApiOperation(value = "图谱-供应商")
    @GetMapping("/get/atlas/supplier")
    public Response<List<UserRelationVO>> getSupplierAtlas() {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getRelationAtlas(userId, UserRelationType.SUPPLIER));
    }

    @ApiOperation(value = "图谱")
    @GetMapping("/get/atlas")
    public Response<List<UserRelationVO>> getAllAtlas() {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getAllRelationAtlas(userId));
    }

    @ApiOperation(value = "获取企业报告")
    @PostMapping("/get/report")
    public Response<CheckEnterpriseReportVO> getReport(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getReport(userId, form));
    }

    @ApiOperation(value = "获取画像-债务")
    @PostMapping("/get/portrait/debt")
    public Response<PortraitDebtVO> getPortraitByDebt(@RequestBody @Validated GetPortraitInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getPortraitByDebt(userId, form));
    }

    @ApiOperation(value = "获取画像-走势")
    @PostMapping("/get/portrait/trend")
    public Response<PortraitTrendVO> getPortraitByTrend(@RequestBody @Validated GetPortraitTrendForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getPortraitByTrend(userId, form));
    }

    @ApiOperation(value = "获取画像-分析")
    @PostMapping("/get/portrait/analyze")
    public Response<PortraitAnalyzeVO> getPortraitByAnalyze(@RequestBody @Validated GetPortraitTrendForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getPortraitByAnalyze(userId, form));
    }

    @ApiOperation(value = "获取画像-诉讼标的额")
    @PostMapping("/get/portrait/amount")
    public Response<PortraitAmountVO> getPortraitByAmount(@RequestBody @Validated GetPortraitAmountForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getPortraitByAmount(userId, form));
    }

    @ApiOperation(value = "获取画像-涉诉关系")
    @PostMapping("/get/portrait/relation")
    public Response<List<PortraitRelationVO>> getPortraitByRelation(@RequestBody @Validated GetPortraitInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getPortraitByRelation(userId, form));
    }

    @ApiOperation(value = "获取画像-地区分布图")
    @PostMapping("/get/portrait/area")
    public Response<PortraitAreaVO> getPortraitByArea(@RequestBody @Validated GetPortraitInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(enterpriseService.getPortraitByArea(userId, form));
    }

    @ApiOperation(value = "获取制裁实体列表")
    @PostMapping("/get/sanction/list")
    public Response<PageVO<SanctionVO>> getSanctionList(@PageableDefault Pageable pageable,
                                                        @NotBlank(message = "企业名称不能为空") @RequestParam String enterpriseName) {
        return ResponseFactory.okResponse(enterpriseService.getSanctionList(enterpriseName, pageable));
    }

    @ApiOperation(value = "获取制裁实体信息-byId")
    @PostMapping("/get/sanction/by-id")
    public Response<SanctionInfoVO> getSanctionById(@RequestBody @Validated GetSanctionInfoForm form) {
        return ResponseFactory.okResponse(enterpriseService.getSanctionById(form));
    }

}
